/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import common.Application;
import localization.Localization;
import templates.PropertyTemplate;

/**
 *
 * @author Alejandro
 */
public class Validator {

    private boolean isValid = false;
    private short resourceUid = 0;
    private String error = "";

    public boolean validate(DatabaseObject obj, short propertyUid) {
        isValid = false;
        error = "";
        resourceUid = propertyUid;
        obj.acceptValidator(this);
        return isValid;
    }

    public String getLastErrorDescription() {
        return error;
    }

    public void validateText(DatabaseText text) {
        PropertyTemplate template = Application.getInstance().getTemplate((short) (resourceUid & 0xff00)).getPropertyTemplate(resourceUid);
        String value = (String) text.getValue();
        int length = Integer.parseInt(template.getParam("length"));
        isValid = true;
        if (value.isEmpty() && !template.isNullEnabled()) {
            isValid = false;
            error = Localization.getString("ValueEmpty");
        } else if (value.length() > length) {
            isValid = false;
            error = String.format(Localization.getString("StringLength"), length);
        }
    }

    public void validateNumber(DatabaseNumber number) {
        PropertyTemplate template = Application.getInstance().getTemplate((short) (resourceUid & 0xff00)).getPropertyTemplate(resourceUid);
        isValid = true;
        if (number.getValue() == null) {
            if (!template.isNullEnabled()) {
                isValid = false;
                error = Localization.getString("ValueEmpty");
            }
            return;
        }
        int value = (Integer) number.getValue();
        int lowerBound = Integer.parseInt(template.getParam("lower-bound"));
        int upperBound = Integer.parseInt(template.getParam("upper-bound"));
        if (value < lowerBound) {
            isValid = false;
            error = String.format(Localization.getString("IntegerLower"), lowerBound);
        } else if (value > upperBound) {
            isValid = false;
            error = String.format(Localization.getString("IntegerGreater"), upperBound);
        }
    }

    public void validateFloat(DatabaseFloat number) {
        PropertyTemplate template = Application.getInstance().getTemplate((short) (resourceUid & 0xff00)).getPropertyTemplate(resourceUid);
        isValid = true;
        if (number.getValue() == null) {
            if (!template.isNullEnabled()) {
                isValid = false;
                error = Localization.getString("ValueEmpty");
            }
            return;
        }
        double value = (Double) number.getValue();
        double lowerBound = Double.parseDouble(template.getParam("lower-bound"));
        double upperBound = Double.parseDouble(template.getParam("upper-bound"));
        if (value < lowerBound) {
            isValid = false;
            error = String.format(Localization.getString("FloatLower"), lowerBound);
        } else if (value > upperBound) {
            isValid = false;
            error = String.format(Localization.getString("FloatGreater"), upperBound);
        }
    }

    public void validateDate(DatabaseDate date) {
        isValid = true;
        if (date.getValue() == null) {
            isValid = false;
            error = Localization.getString("ValueEmpty");
        }
    }

    void validateEmail(DatabaseEmail email) {
        String regex="(([0-9a-zA-Z][.\\-\\w]*)@((?:[0-9a-zA-Z][\\-0-9a-zA-Z]*[0-9a-zA-Z])+\\.{1}[a-z]{2,6}))";
        PropertyTemplate template = Application.getInstance().getTemplate((short) (resourceUid & 0xff00)).getPropertyTemplate(resourceUid);
        String value = (String) email.getValue();
        int length = Integer.parseInt(template.getParam("length"));
        isValid = true;
        if (value.isEmpty() && !template.isNullEnabled()) {
            isValid = false;
            error = Localization.getString("ValueEmpty");
        } else if (value.isEmpty() && template.isNullEnabled()) {
            return;
        } else if (value.length() > length) {
            isValid = false;
            error = String.format(Localization.getString("EmailLength"), length);
        } else if (!value.matches(regex)) {
            isValid = false;
            error = Localization.getString("EmailIncorrect");
        }
    }

    void validatePhone(DatabasePhone phone) {
        String regex="(^((8|\\+7)[\\- ]?)?(\\(?\\d{3}\\)?[\\- ]?)?[\\d\\- ]{7,10}$)";
        PropertyTemplate template = Application.getInstance().getTemplate((short) (resourceUid & 0xff00)).getPropertyTemplate(resourceUid);
        String value = (String) phone.getValue();
        int length = Integer.parseInt(template.getParam("length"));
        isValid = true;
        if (value.isEmpty() && !template.isNullEnabled()) {
            isValid = false;
            error = Localization.getString("ValueEmpty");
        } else if (value.isEmpty() && template.isNullEnabled()) {
            return;
        } else if (value.length() > length) {
            isValid = false;
            error = String.format(Localization.getString("PhoneLength"), length);
        } else if (!value.matches(regex)) {
            isValid = false;
            error = Localization.getString("PhoneIncorrect");
        }
    }
}
